package be.kuleuven.mume.arrayAdapters;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.Vak;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class VakSelectAdapter extends ArrayAdapter<Vak> {

	public HashMap<Integer,Vak> checked = new HashMap<Integer,Vak>();

	private List<Vak> vakken;
	
	public VakSelectAdapter(Context context, List<Vak> objects) {
		super(context, R.layout.rij_agenda, objects);
		vakken = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.rij_select, null);
		}

		Vak v = vakken.get(position);
		if (v == null)
			return view;
		
		setViewText(view, R.id.rij_checkbox, v.getName());
		CheckBox b = (CheckBox)view.findViewById(R.id.rij_checkbox);
		b.setChecked(checked.containsKey(position));
		return view;
	}
	
	private void setViewText(View view, int viewId, String text) {
		TextView textv = (TextView) view.findViewById(viewId);
		if (textv != null) {
			textv.setText(text);
		}
	}

	public void setChecked(int position) {
        if (checked.containsKey(position)){
            checked.remove(position);
        }

        else {
            checked.put(position, getItem(position));
        }
        notifyDataSetChanged();
	}
	
	public Collection<Vak> getChechedItems(){
		return checked.values();
	}
	
}
